#include"udpsocket.hpp"
#include<string>
#include<stdlib.h>
#include<cstdlib>
using namespace std;
#include<unistd.h>

#define CHECK_RET(q) if((q)==false) {return -1;}


int main(int argc,char*argv[])
{
  //通过运行参数指定服务端地址信息
  if(argc < 3){
      cout << "usage:请输入服务端地址信息\n！" ;  
      cout << "\t ./udp_cli 192.168.1.2 9000\n";
      return -1;
  }
  string srv_ip = argv[1];
 int  srv_port = atoi(argv[2]);//stoi 字符串转整形
  //1.创建套接字
  UdpSocket sock;
  CHECK_RET( sock.Socket());

  

  //2.绑定地址信息（不推荐绑定）
  

  //3.发送请求
  while(1)
  {
    string buf;
    cout<<"client input:";
    cin>>buf;//从标准输入获取数据
    CHECK_RET(sock.Send(buf,srv_ip, srv_port));
  
  //4.接收响应
  buf.clear();
  CHECK_RET(sock.Recv(&buf));
  cout << "server respomse: " <<  buf << endl;
  }
  //5.关闭套接字
 sock.Close();


  return 0;
}
